// we7/pages/mine/myOrder.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    limit:10,
    state:1,
    orderList:'',
    type:1,
    noMoreList:false,
    order_type:"",
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    order_id:"",
    order_no:"",
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.type){
      this.setData({
        state: options.type,
        page: 1,
        type: options.type
      })
    }
    if (options.ordertype){
      this.setData({
        state: options.ordertype,
        page: 1,
        type: options.ordertype
      })
    }
    wx.setNavigationBarTitle({
      title: '订单列表',
    })
    const value = wx.getStorageSync('openid');
    var that=this
    app.util.request({
      url: 'admin/WxGoodsApi/orderList',
      data: {
        uid: value,
        page: that.data.page,
        limit: that.data.limit,
        type: that.data.type
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          var data = res.data.data
          for (var i = 0; i < data.length; i++) {
            data[i]['goods_image'] = app.globalData.url + data[i]['goods_image'];
          }
          that.setData({
            orderList: data,
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  getOrderList:function(e){
    // console.log(e.currentTarget.dataset.id)
    var that = this
    var type = e.currentTarget.dataset.id
    var page=1
    that.setData({ 
      state: type,
      page:1 
    })
    const value = wx.getStorageSync('openid');

    app.util.request({
      url: 'admin/WxGoodsApi/orderList',
      data: {
        uid: value,
        page: page,
        limit: that.data.limit,
        type: type
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
            var data = res.data.data
            for (var i = 0; i < data.length; i++) {
              data[i]['goods_image'] = app.globalData.url + data[i]['goods_image'];
            }
            that.setData({
              orderList: data,
              type:type,
            })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  // 确认收货之前
  queBox(e){
    // console.log(e.currentTarget.dataset.index)
    var that = this
    that.showModal(e)
    var index = e.currentTarget.dataset.index
    var order_id = that.data.orderList[index].id
    that.setData({
      order_id: order_id
    })
  }, 
  // 确认收货
  queOrder(e){
    
    var that = this
    const value = wx.getStorageSync('openid');

    app.util.request({
      url: 'admin/WxGoodsApi/confirmOrCancelOrder',
      data: {
        order_id: that.data.order_id,
        uid: value,
        type:1
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          that.hideModal(e)
          setTimeout(function () {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }, 200)
          that.onLoad()
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  // 删除订单之前
  delBox(e){
    
    // console.log(e.currentTarget.dataset.index)
    var that = this
    that.showModal(e)
    var index = e.currentTarget.dataset.index
    var order_id = that.data.orderList[index].id
    that.setData({
      order_id: order_id
    })
  },
  // 删除订单
  delOrder(e){
    var that = this
    const value = wx.getStorageSync('openid');
    app.util.request({
      url: 'admin/WxGoodsApi/delOrder',
      data: {
        id: that.data.order_id
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          that.hideModal(e)
          setTimeout(function(){
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          },200)
          that.onLoad()
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  // 取消订单之前
  cancelBox(e){
    var that = this
    that.showModal(e)
    var index = e.currentTarget.dataset.index
    var order_id = that.data.orderList[index].id
    that.setData({
      order_id: order_id
    })
  },
  // 取消订单
  cancelOrder(e){
    var that = this
    const value = wx.getStorageSync('openid');

    app.util.request({
      url: 'admin/WxGoodsApi/confirmOrCancelOrder',
      data: {
        order_id: that.data.order_id,
        uid: value,
        type: 2
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          that.hideModal(e)
          setTimeout(function () {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }, 200)
          that.onLoad()
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  // 支付订单之前

  zhiBox(e){
    var that = this
    that.showModal(e)
    var index = e.currentTarget.dataset.index
    var order_no = that.data.orderList[index].order_no
    that.setData({
      order_no: order_no
    })
  },
  // 支付订单
  zhiOrder(e){
    var that = this
    const value = wx.getStorageSync('openid');

    app.util.request({
      url: 'admin/WxGoodsApi/confirmPay',
      data: {
        order_no: that.data.order_no,
        uid: value
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          that.hideModal(e)
          // 拉起支付
          app.startpay(res.data.data)
          // setTimeout(function () {
          //   wx.showToast({
          //     title: res.data.msg,
          //     icon: 'none',
          //     duration: 2000
          //   })
          // }, 200)
        }
  },
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const value = wx.getStorageSync('openid');
    var that=this
    var page = that.data.page
    var pages=++page
    that.setData({
      page:pages
    })
    // console.log(pages)
    app.util.request({
      url: 'admin/WxGoodsApi/orderList',
      data: {
        uid: value,
        page: pages,
        limit: that.data.limit,
        type: that.data.type
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res.data.data)

        if (res.data.code == 0) {
          var data = res.data.data
          if (data === undefined || data.length == 0){
            that.setData({
              noMoreList:true
            })
          }else{
            for (var i = 0; i < data.length; i++) {
              data[i]['goods_image'] = app.globalData.url + data[i]['goods_image'];
            }
            var orderList = that.data.orderList
            for (var p = 0; p < data.length; p++) {
              orderList.push(data[p])
            }

            that.setData({
              orderList: orderList,
            })
          }
          
        }


      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})